﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Process.DAL;
using Process.Entities;
using KZ.Exception;

namespace Process.BLL
{
    public class InventoryBLL
    {
        public static List<OrderDetailENT> GetUpdateInventoryNotYetDone(string providerID)
        {
            return OrderDetailDAL.GetOrderDetailByOrderStatus(OrderStatus.Draft, providerID);
        }

        public static int CheckInventory(string transID)
        {
            return OrderDetailDAL.GetInventoryStockingQuantity(transID);
        }

        public static int UpdateInventory(string transID, int addedQuantity, decimal unitPrice, decimal transportPrice, decimal totalPrice)
        {
            TransactionENT trans = TransactionDAL.GetTransactionByID(transID);
            if (String.IsNullOrEmpty(trans.TransID)) { throw new BusinessLogicException("Giao dịch không tồn tại, không thể thực hiện việc cập nhật kho."); }

            OrderENT order = new OrderENT
            {
                OrderID = Guid.NewGuid().ToString(),
                OrderDate = DateTime.Today,
                OrderType = OrderType.Purchase.ToString(),
                PartnerID = trans.ProviderID,
                GrandTotal = totalPrice,
                OrderStatus = OrderStatus.Draft.ToString()
            };

            order.OrderDetaiList = new List<OrderDetailENT>();
            order.OrderDetaiList.Add(new OrderDetailENT
            {
                OrderDetailID = Guid.NewGuid().ToString(),
                OrderID = order.OrderID,
                TransID = transID,
                ProductID = trans.ProductID,
                Quantity = addedQuantity,
                UnitPrice = unitPrice,
                TransportPrice = transportPrice,
                LineNet = totalPrice
            });

            OrderDAL orderDAL = new OrderDAL();
            orderDAL.InsertOrderAndDetail(order);

            return 0;
        }
    }
}
